/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.pconline.activity.service;

import cn.pconline.activity.domain.Admin;
import cn.pconline.activity.domain.Pager;
import cn.pconline.activity.domain.User;
import cn.pconline.activity.repository.GenericRepository;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author pc
 */
public class UserService {

    @Autowired
    GenericRepository genericRepository;

    public long createUser(User user) {
        return genericRepository.create(user);
    }

    public void updateUser(User user) {
        genericRepository.update(user);
    }

    public void deleteUser(User user) {
        genericRepository.delete(user);
    }

    public void deleteUser(long userId) {
        genericRepository.delete(userId, User.class);
    }

    public long createAdmin(Admin admin) {
        return genericRepository.create(admin);
    }

    public Pager<User> page(String where, int pageNo, int pageSize, Map<String, Object> params) {
        StringBuilder sql = new StringBuilder("select * from ");
        StringBuilder sqlCount = new StringBuilder("select count(*) from ");
        sql.append(genericRepository.getTablePreFix()).append("user where 1 = 1 ").append(where);
        sqlCount.append(genericRepository.getTablePreFix()).append("user where 1 = 1 ").append(where);

        Pager<User> pager = new Pager<User>();
        pager.setPageNo(pageNo);
        pager.setPageSize(pageSize);
        pager.setResultList(genericRepository.page(User.class, sql.toString(), pageNo, pageSize, params));
        pager.setTotal(genericRepository.count(sqlCount.toString(), params));
        return pager;
    }

    
}
